﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateDemo2
{
    //定义委托
    public delegate string GeneralEventHandler();

    //定义事件发布者
    public class Publisher
    {
        //声明一个事件
        public event GeneralEventHandler NumberChanged;
        //触发事件
        public void DoSomething()
        {
            if (NumberChanged != null)
            {
                string rtn = NumberChanged();
                Console.WriteLine(rtn);
            }
        }
    }

    //定义事件订阅者
    public class Subscriber1
    {
        public string OnNumberChanged()
        {
            return "Subscriber1";
        }
    }
    public class Subscriber2
    {
        public string OnNumberChanged()
        {
            return "Subscriber2";
        }
    }
    public class Subscriber3
    {
        public string OnNumberChanged()
        {
            return "Subscriber3";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Publisher pub = new Publisher();
            Subscriber1 sub1 = new Subscriber1();
            Subscriber2 sub2 = new Subscriber2();
            Subscriber3 sub3 = new Subscriber3();

            pub.NumberChanged += new GeneralEventHandler(sub1.OnNumberChanged);
            pub.NumberChanged += new GeneralEventHandler(sub2.OnNumberChanged);
            pub.NumberChanged += new GeneralEventHandler(sub3.OnNumberChanged);
			//得到的输出是Subscriber3，只得到了最后一个方法的返回值
            pub.DoSomething();
        }
    }
}
